<?php
/**
 * Created by PhpStorm.
 * User: 86188
 * Date: 2021/12/12
 * Time: 17:18
 */

//1.创建连接
$conn=new mysqli("127.0.0.1","root","123456","bank");
//2.编写sql语句
$sql="select * from bankcard";
//3.执行Sql 同时将返回结果封装到变量中
$result = $conn->query($sql);
//4.创建数组
$data=[];
//5.循环遍历
while(true){
    //5.1 将获取的结果封装到变量中
    $row = $result->fetch_assoc();
    //5.2 判断变量是否为空
    if ($row === null) {
        //5.2.1 如果变量为空则跳出循环
        break;
    }
    //5.3将结果封装到数组中的最后一个索引
    $data[]=$row;
}
//打印数组
print_r($data);
class SqlUtils
{
    public static $conn;//执行Sql语句需要一个连接

    public static function select($sql){
        $result = self::$conn->query($sql);
        $data=[];
        while(true){
            $row = $result->fetch_assoc();
            if ($row === null) {
                break;
            }
            $data[]=$row;
        }
        return $data;
    }
}
//使用工具类：
//require "../common/SqlUtils.php";
require_once '../common/SqlUtils.php';
//为连接属性赋值
SqlUtils::$conn=new mysqli("127.0.0.1","root","123456","bank");
//编写Sql字符串
$sql="select * from bankcard";
//打印工具类方法执行结果
print_r(SqlUtils::select($sql));